package org.jeecg.modules.inventory.inventoryInfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.constant.WarehouseConstant;
import org.jeecg.modules.inventory.inventoryInfo.entity.InventoryInfo;
import org.jeecg.modules.inventory.inventoryInfo.service.IInventoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 库存信息
 * @Author: jeecg-boot
 * @Date: 2024-11-29
 * @Version: V1.0
 */
@Api(tags = "库存信息")
@RestController
@RequestMapping("/inventoryInfo/inventoryInfo")
@Slf4j
public class InventoryInfoController extends JeecgController<InventoryInfo, IInventoryInfoService> {
    @Autowired
    private IInventoryInfoService inventoryInfoService;

    /**
     * 分页列表查询
     *
     * @param inventoryInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "库存信息-分页列表查询")
    @ApiOperation(value = "库存信息-分页列表查询", notes = "库存信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(InventoryInfo inventoryInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<InventoryInfo> queryWrapper = QueryGenerator.initQueryWrapper(inventoryInfo, req.getParameterMap());
        Page<InventoryInfo> page = new Page<InventoryInfo>(pageNo, pageSize);
        IPage<InventoryInfo> pageList = inventoryInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param inventoryInfo
     * @return
     */
    @AutoLog(value = "库存信息-添加")
    @ApiOperation(value = "库存信息-添加", notes = "库存信息-添加")
//    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody InventoryInfo inventoryInfo) {
        inventoryInfoService.save(inventoryInfo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param inventoryInfo
     * @return
     */
    @AutoLog(value = "库存信息-编辑")
    @ApiOperation(value = "库存信息-编辑", notes = "库存信息-编辑")
//    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody InventoryInfo inventoryInfo) {
        inventoryInfoService.updateById(inventoryInfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "库存信息-通过id删除")
    @ApiOperation(value = "库存信息-通过id删除", notes = "库存信息-通过id删除")
//    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        inventoryInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "库存信息-批量删除")
    @ApiOperation(value = "库存信息-批量删除", notes = "库存信息-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.inventoryInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "库存信息-通过id查询")
    @ApiOperation(value = "库存信息-通过id查询", notes = "库存信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        InventoryInfo inventoryInfo = inventoryInfoService.getById(id);
        if (inventoryInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(inventoryInfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param inventoryInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, InventoryInfo inventoryInfo) {
        return super.exportXls(request, inventoryInfo, InventoryInfo.class, "库存信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, InventoryInfo.class);
    }

    /**
     * 出库页面，获取货道编码列表
     *
     * @return
     */
    @PostMapping(value = "/getRoadCodeList")
    public Result<?> getRoadCodeList() {
        List<InventoryInfo> list = inventoryInfoService.list();
        Set<String> roadCodeSet = list.stream().map(InventoryInfo::getRoadCode).collect(Collectors.toSet());
        return Result.OK(roadCodeSet);
    }

    /**
     * 出库页面，获取货垛编码列表
     *
     * @return
     */
    @PostMapping(value = "/getStackCodeList")
    public Result<?> getStackCodeList(@RequestParam(name = "roadCode") String roadCode) {
        List<InventoryInfo> list = inventoryInfoService.list(new LambdaQueryWrapper<InventoryInfo>()
                .eq(InventoryInfo::getWarehouseCode, WarehouseConstant.WarehouseTypeEnum.ROAD.getCode())
                .eq(InventoryInfo::getRoadCode, roadCode));
        Set<String> stackCodeSet = list.stream().map(InventoryInfo::getStackCode).collect(Collectors.toSet());
        return Result.OK(stackCodeSet);
    }

    /**
     * 出库页面，获取品番编码列表
     *
     * @return
     */
    @PostMapping(value = "/getSkuCodeList")
    public Result<?> getSkuCodeList(@RequestParam(name = "roadCode") String roadCode, @RequestParam(name = "stackCode") String stackCode) {
        List<InventoryInfo> list = inventoryInfoService.list(new LambdaQueryWrapper<InventoryInfo>()
                .eq(InventoryInfo::getWarehouseCode, WarehouseConstant.WarehouseTypeEnum.ROAD.getCode())
                .eq(InventoryInfo::getRoadCode, roadCode)
                .eq(InventoryInfo::getStackCode, stackCode));
        Set<String> skuCodeSet = list.stream().map(InventoryInfo::getSkuCode).collect(Collectors.toSet());
        return Result.OK(skuCodeSet);
    }

    /**
     * 出库页面，获取剩余库存数量
     *
     * @return
     */
    @PostMapping(value = "/getAvailableBoxQuantity")
    public Result<?> getAvailableBoxQuantity(@RequestParam(name = "roadCode") String roadCode,
                                             @RequestParam(name = "stackCode") String stackCode,
                                             @RequestParam(name = "skuCode") String skuCode) {
        InventoryInfo inventoryInfo = inventoryInfoService.getOne(new LambdaQueryWrapper<InventoryInfo>()
                .eq(InventoryInfo::getWarehouseCode, WarehouseConstant.WarehouseTypeEnum.ROAD.getCode())
                .eq(InventoryInfo::getRoadCode, roadCode)
                .eq(InventoryInfo::getStackCode, stackCode)
                .eq(InventoryInfo::getSkuCode, skuCode));
        return Result.OK(inventoryInfo.getAvailableBoxQuantity());
    }
}
